<template>
    <el-card shadow="always" :body-style="{ padding: '20px', height: '700px' }" v-loading="loading">
        <template #header>
            <span>蜘蛛数据统计</span>
            <el-button type="primary" size="small" @click="spiderData" class="title-btn">加载数据</el-button>
        </template>
        <ECharts ref="chart" :option="option"/>
    </el-card>
</template>

<script>
    import { h } from 'vue';
    import * as echarts from 'echarts';
    import { createComponent } from 'echarts-for-vue';
    import axios from 'axios'

    export default {
        name: 'SpiderData',
        components: {
            ECharts: createComponent({ echarts, h }), 
        },
        data() {
            return {
                loading: false,
                dateReady: false,
                option: {
                     title: {
                        text: '蜘蛛访问统计',
                        subtext: '数据未加载',
                        left: 'center'
                    },
                    tooltip: {
                        trigger: 'item',
                        formatter: '{a} <br/>{b} : {c} ({d}%)'
                    },
                    legend: {
                        orient: 'vertical',
                        left: 'left',
                        data: ['百度蜘蛛', '谷歌蜘蛛', '360蜘蛛', '搜狗蜘蛛', '必应蜘蛛']
                    },
                    series: [
                        {
                            name: '访问来源',
                            type: 'pie',
                            radius: '55%',
                            center: ['50%', '60%'],
                            data: [
                                {value: 0, name: '百度蜘蛛'},
                                {value: 0, name: '谷歌蜘蛛'},
                                {value: 0, name: '360蜘蛛'},
                                {value: 0, name: '搜狗蜘蛛'},
                                {value: 0, name: '必应蜘蛛'},
                            ],
                            emphasis: {
                                itemStyle: {
                                    shadowBlur: 10,
                                    shadowOffsetX: 0,
                                    shadowColor: 'rgba(0, 0, 0, 0.5)'
                                }
                            }
                        }
                    ]
                },
            };
        },
        methods: {
            async spiderData(){
                this.loading = true;
                let res = await axios.get('http://localhost/api/usage/getSpiderUsage')
                .then(res => res.data)
                .catch(err => {
                    console.error(err); 
                })
                this.loading = false;
                res.data.forEach((item, index)=>{
                    this.option.series[0].data[index].value = item.value
                })
                this.option.title.subtext = res.yestoday
               
                this.charts.setOption(this.option, true, false)
            }
        },
        mounted() {
            this.charts = this.$refs.chart
        },
    }
</script>


<style lang="scss" scoped>
.title-btn {
    float: right;
    margin-right: 20px;
}
</style>